/**  * Created by nena on 2018/7/20.  */
let {
    PREFIX,

    GROUPID,
    AGENTID,

    QUEUE,
    AGENTS,
    GROUPS,
    STATUS,
    ONLINE,
    DOMAINS,
    REQUESTS,

    WORKING_AGENTS,
    LAST_DISTRIBUTE_AGENT,
    LASTTIME
} = require("../utils/constants");

/*data : {
    domains: boolean,
    domain: string,
    all: boolean,
    groups: boolean,
    group: {
        id: string,
        queue: boolean,
        agents: boolean,
        lastDistributeAgent: boolean,
        all: boolean
    },
    workingAgents: boolean
    agent: {
        id: string,
        online: boolean,
        groups: boolean,
        status: boolean,
    },
}*/

module.exports = function (data) {
    let key = null;
    if (data.domains) {
        key = PREFIX + DOMAINS
    } else if (data.domain) {
        key = PREFIX + data.domain
        if (data.groups) {
            key += GROUPS
        }else if (data.group) {
            key += GROUPID + data.group.id
            if (data.group.queue) key += QUEUE
            else if (data.group.agents) key += AGENTS
            else if (data.group.lastDistributeAgent) key += LAST_DISTRIBUTE_AGENT
            else if (!data.group.all) key = null
        }else if (data.workingAgents) {
            key += WORKING_AGENTS
        } else if (data.agent && data.agent.id) {
            key += AGENTID + data.agent.id
            if (data.agent.groups) key += GROUPS
            else if (data.agent.status) key += STATUS
            else if (data.agent.online) key += ONLINE
        } else if (data.lastDistributeTime) {
            key += LASTTIME
        } else if (data.requests && data.requests.id) {
            key += REQUESTS + data.requests.id
            if (data.requests.groups) key += GROUPS
        } else if (!data.all){
            key = null
        }
    }
    // console.log("getKey:", key)
    if (key === null) console.error("rediskeyMaker.getKey ERROR !!!!!", data);
    return key;
}

